package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorAppBanner;
import guoanmaker.operator.business.model.OperatorServerClass;
import guoanmaker.operator.business.repository.OperatorAppBannerRepository;
import guoanmaker.operator.business.repository.OperatorServerClassRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.transaction.Transactional;

/**
 *  
 *
 * @ClassName: OperatorAppBannerService 
 * @Description: OperatorAppBannerService 
 * @author: huZhaoyu 
 * @date 2017/6/28 下午2:56 
 */
@Service
public class OperatorAppBannerService implements OperatorAppBannerServiceInterface {
    private final OperatorAppBannerRepository operatorAppBannerRepository;
    private final OperatorServerClassRepository operatorServerClassRepository;

    @Override
    public List<OperatorAppBanner> getAllAndServerName() {
        List<OperatorAppBanner> alls = operatorAppBannerRepository.findAll();
//        ArrayList<OperatorAppBanner> banners = new ArrayList(Arrays.asList(alls));
        for (OperatorAppBanner  banner : alls){
            OperatorServerClass operatorServerClass = operatorServerClassRepository.findOne(banner.getServerId());
            banner.setServerId(operatorServerClass.getServerName());
        }
        return alls;
    }

    @Override
    public Integer countAllById() {
        return operatorAppBannerRepository.countAllByIdIsNotNull();
    }

    @Override
    public Page<OperatorAppBanner> getAppBannerByPageOrderByWeight(Integer page, Integer size) {
        return operatorAppBannerRepository.findAll(new PageRequest(page, size, new Sort(Sort.Direction.DESC, "weight")));
    }

    @Autowired
    public OperatorAppBannerService(OperatorAppBannerRepository operatorAppBannerRepository, OperatorServerClassRepository operatorServerClassRepository) {
        this.operatorAppBannerRepository = operatorAppBannerRepository;
        this.operatorServerClassRepository = operatorServerClassRepository;
    }

    @Override
    @Transactional
    public OperatorAppBanner saveAppBanner(OperatorAppBanner operatorAppBanner) {
        return operatorAppBannerRepository.save(operatorAppBanner);
    }

    @Override
    public List<OperatorAppBanner> getAllAppBanner(String cityId) {
    	List<OperatorAppBanner> bans= operatorAppBannerRepository.findBycityCode(cityId);
    	for(OperatorAppBanner ban:bans){
    		
    	if(ban.getIsHot()==1){
    		ban.setFasehot(true);
    	}else{
    		ban.setFasehot(false);
    	}
    	if(ban.getIsNew()==1){
    		ban.setFasenew(true);
    	}else{
    		ban.setFasenew(false);
    	}
    	if(ban.getItemLevel()!=null&&!ban.getItemLevel().equals("")){
    	if(ban.getItemLevel()==1){
    		ban.setCopyitemLevel("1");
    	}else{
    		ban.setCopyitemLevel("2");
    	}
    	}
    	}
    	return bans;
    }

    @Override
    public Page<OperatorAppBanner> getAllAppBannerByPage(Integer page, Integer size) {
        return operatorAppBannerRepository.findAll(new PageRequest(page, size));
    }

    @Override
    @Transactional
    public void deleteAppBannerById(String id) {
        operatorAppBannerRepository.delete(id);
    }

    @Override
    public OperatorAppBanner getAppBannerById(String id) {
        return operatorAppBannerRepository.findOne(id);
    }
    
    @Override
    @Transactional
    public void deleteBannerById(String id) {
        operatorAppBannerRepository.delete(id);;
    }
}
